﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Windows.ApplicationModel;
using Windows.ApplicationModel.Store;
using Windows.Storage;
using Windows.UI.Popups;
using Windows.UI.Xaml;

namespace Books.Common
{
    public class HandleInAppPurchase
    {
        LicenseChangedEventHandler licenseChangeHandler = null;
        public async Task<bool> LoadInAppPurchaseAsync(string productId)
        {
            LicenseInformation licenseInformation = CurrentApp.LicenseInformation;
            ListingInformation productListing = await CurrentApp.LoadListingInformationAsync();
            ProductListing productListingData = null;
            if (productListing.ProductListings.TryGetValue(productId, out productListingData))
            {
                Window.Current.Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () =>
                  {
                      MessageDialog message = new MessageDialog("Could not find product information", "Message");
                      message.ShowAsync();
                  });

                return false;
            }
            else
            {
                if (!licenseInformation.ProductLicenses[productId].IsActive)
                {
                    try
                    {
                        ListingInformation li = await CurrentApp.LoadListingInformationAsync();
                        string pID = li.ProductListings[productId].ProductId;
                        string receipt = await CurrentApp.RequestProductPurchaseAsync(pID, false);
                        return true;
                    }
                    catch (Exception ex)
                    {
                        Window.Current.Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () =>
                        {
                            MessageDialog message = new MessageDialog(ex.Message, "Message");
                            message.ShowAsync();
                        });
                        return false;
                    }
                }

            }
            return false;

        }
        public async Task<bool> LoadInAppPurchaseProxyFileAsync(string productId)
        {

            StorageFolder proxyDataFolder = await Package.Current.InstalledLocation.GetFolderAsync("data");
            StorageFile proxyFile = await proxyDataFolder.GetFileAsync("in-app-purchase.xml");
            licenseChangeHandler = new LicenseChangedEventHandler(InAppPurchaseRefreshScenario);
            CurrentAppSimulator.LicenseInformation.LicenseChanged += licenseChangeHandler;
            //await CurrentAppSimulator.ReloadSimulatorAsync(proxyFile);

            LicenseInformation licenseInformation = CurrentAppSimulator.LicenseInformation;
            ListingInformation productListing = await CurrentAppSimulator.LoadListingInformationAsync();

            ProductListing productListingData = null;
            if (!productListing.ProductListings.TryGetValue(productId, out productListingData))
            {
                Window.Current.Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () =>
                {
                    MessageDialog message = new MessageDialog("Could not find product information", "Message");
                    message.ShowAsync();
                });
                return false;
            }
            else
            {
                if (!licenseInformation.ProductLicenses[productId].IsActive)
                {
                    MessageDialog message = new MessageDialog("Buying Product" + " " + productId, "Message");
                    message.ShowAsync();
                    try
                    {
                        await CurrentAppSimulator.RequestProductPurchaseAsync(productId);
                        //if (licenseInformation.ProductLicenses[productId].IsActive)
                        //{
                            Window.Current.Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () =>
                            {
                                MessageDialog messagea = new MessageDialog("You bought Product" + " " + productId, "Message");
                                messagea.ShowAsync();
                            });
                          
                            return true;
                        //}
                        //else
                        //{
                        //    Window.Current.Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () =>
                        //    {
                        //        MessageDialog messagea = new MessageDialog("Product " + productId + " was not purchased.", "Message");
                        //        messagea.ShowAsync();
                        //    });
                          
                        //    return false;
                        //}
                    }
                    catch (Exception ex)
                    {
                        Window.Current.Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () =>
                        {
                            MessageDialog messagefail = new MessageDialog(ex.Message, "Message");
                            messagefail.ShowAsync();
                        });
                       
                        return false;
                    }
                }
                else
                {
                    Window.Current.Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () =>
                    {
                        MessageDialog messagefail = new MessageDialog("You already own Product" + productId, "Message");
                        messagefail.ShowAsync();
                    });
                  
                    return true;

                }
            }
        }

        private void InAppPurchaseRefreshScenario()
        {

        }

    }
}
